package es.efor.ics.gev.model;

import java.io.Serializable;

import javax.persistence.*;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Una {@link Accion accion} en una {@link Fase fase} + obligatoriedad + orden
 * 
 * @see Fase
 * @see Accion
 */
@SuppressWarnings("unchecked")
@Entity
@Table(name = "FASES_ACCIONES")
public class AccionAsignada extends BaseObject
		implements
			Serializable,
			Comparable {

	/**
	 * Embedded composite identifier class that represents the primary key
	 * columns of the many-to-many join table.
	 */
	@Embeddable
	public static class Id implements Serializable {
		private static final long serialVersionUID = 3278121336943515787L;

		private Long faseId;
		private Long accionId;

		public Id() {
		}

		public Id(Long faseId, Long accionId) {
			this.faseId = faseId;
			this.accionId = accionId;
		}

		@Column(name = "FASE_ID")
		public Long getFaseId() {
			return faseId;
		}

		public void setFaseId(Long faseId) {
			this.faseId = faseId;
		}

		@Column(name = "ACCION_ID")
		public Long getAccionId() {
			return accionId;
		}

		public void setAccionId(Long accionId) {
			this.accionId = accionId;
		}

		@Override
		public boolean equals(Object o) {
			if (o instanceof Id) {
				Id that = (Id) o;
				return this.faseId.equals(that.faseId)
						&& this.accionId.equals(that.accionId);
			} else {
				return false;
			}
		}

		@Override
		public int hashCode() {
			return faseId.hashCode() + accionId.hashCode();
		}

		@Override
		public String toString() {
			return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
					.append("faseId", this.getFaseId()).append("accionId",
							this.getAccionId()).toString();
		}

	}

	// static data
	private static final long serialVersionUID = -3537547759250458563L;

	// private instance data
	private Id id;
	private Fase fase;
	private Accion accion;
	private Integer orden;
	private boolean obligatoria;
	private Integer version;

	/**
	 * Construct an uninitialized <code>AccionAsignada</code> (for runtime proxy
	 * generation in Hibernate).
	 */
	protected AccionAsignada() {
	}

	/**
	 * Construct an initialized <code>AccionAsignada</code>
	 * 
	 * @param fase
	 * @param accion
	 * @param orden
	 * @param obligatoria
	 */
	public AccionAsignada(Fase fase, Accion accion, boolean obligatoria,
			Integer orden) {
		this.fase = fase;
		this.accion = accion;
		this.orden = orden;
		this.obligatoria = obligatoria;

		// Set primary key
		this.id = new Id(fase.getId(), accion.getId());
	}

	// ********************** Accessor Methods ********************** //

	@EmbeddedId
	public Id getId() {
		return id;
	}

	public void setId(Id id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "FASE_ID", insertable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_FASES_ACCIONES_FASE_ID")
	public Fase getFase() {
		return fase;
	}

	public void setFase(Fase fase) {
		this.fase = fase;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ACCION_ID", insertable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_FASES_ACCIONES_ACCION_ID")
	public Accion getAccion() {
		return accion;
	}

	public void setAccion(Accion accion) {
		this.accion = accion;
	}

	@Column(name = "ORDEN", nullable = false)
	public Integer getOrden() {
		return this.orden;
	}

	public void setOrden(Integer orden) {
		this.orden = orden;
	}

	@Column(name = "OBLIGATORIA", nullable = false)
	public boolean isObligatoria() {
		return obligatoria;
	}

	public void setObligatoria(boolean obligatoria) {
		this.obligatoria = obligatoria;
	}
	
	@Version
	@Column(name = "OBJ_VERSION")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	// ********************** Common Methods ********************** //

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		final AccionAsignada that = (AccionAsignada) o;
		return id.equals(that.id);
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append(
				"fase", this.fase.getNombreFase()).append("accion",
				this.accion.getNombreAccion()).append("orden", this.getOrden())
				.append("obligatoria", this.isObligatoria()).toString();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Object o) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;
		
		AccionAsignada aThat = (AccionAsignada) o;
		// this optimization is usually worthwhile, and can
		// always be added
		if (this == aThat)
			return EQUAL;

		// primitive numbers follow this form
		if (this.orden < aThat.orden)
			return BEFORE;
		if (this.orden > aThat.orden)
			return AFTER;

		// all comparisons have yielded equality
		// verify that compareTo is consistent with equals (optional)
		assert this.equals(aThat) : "compareTo inconsistent with equals.";

		return EQUAL;
	}

}
